package cn.iocoder.yudao.module.kt.service.resource;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.kt.controller.admin.resource.vo.*;
import cn.iocoder.yudao.module.kt.dal.dataobject.resource.ResourceDO;
import cn.iocoder.yudao.module.kt.dal.dataobject.jiaocai.JiaocaiDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.kt.dal.mysql.resource.ResourceMapper;
import cn.iocoder.yudao.module.kt.dal.mysql.jiaocai.JiaocaiMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.kt.enums.ErrorCodeConstants.*;

/**
 * 教材分类 Service 实现类
 *
 * @author zhaokang
 */
@Service
@Validated
public class ResourceServiceImpl implements ResourceService {

    @Resource
    private ResourceMapper resourceMapper;
    @Resource
    private JiaocaiMapper jiaocaiMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createResource(ResourceSaveReqVO createReqVO) {
        // 插入
        ResourceDO resource = BeanUtils.toBean(createReqVO, ResourceDO.class);
        resourceMapper.insert(resource);

        // 插入子表
        createJiaocaiList(resource.getId(), createReqVO.getJiaocais());
        // 返回
        return resource.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateResource(ResourceSaveReqVO updateReqVO) {
        // 校验存在
        validateResourceExists(updateReqVO.getId());
        // 更新
        ResourceDO updateObj = BeanUtils.toBean(updateReqVO, ResourceDO.class);
        resourceMapper.updateById(updateObj);

        // 更新子表
        updateJiaocaiList(updateReqVO.getId(), updateReqVO.getJiaocais());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteResource(Long id) {
        // 校验存在
        validateResourceExists(id);
        // 删除
        resourceMapper.deleteById(id);

        // 删除子表
        deleteJiaocaiByResourceId(id);
    }

    private void validateResourceExists(Long id) {
        if (resourceMapper.selectById(id) == null) {
            throw exception(RESOURCE_NOT_EXISTS);
        }
    }

    @Override
    public ResourceDO getResource(Long id) {
        return resourceMapper.selectById(id);
    }

    @Override
    public PageResult<ResourceDO> getResourcePage(ResourcePageReqVO pageReqVO) {
        return resourceMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（教材资源） ====================

    @Override
    public List<JiaocaiDO> getJiaocaiListByResourceId(Long resourceId) {
        return jiaocaiMapper.selectListByResourceId(resourceId);
    }

    private void createJiaocaiList(Long resourceId, List<JiaocaiDO> list) {
        list.forEach(o -> o.setResourceId(resourceId));
        jiaocaiMapper.insertBatch(list);
    }

    private void updateJiaocaiList(Long resourceId, List<JiaocaiDO> list) {
        deleteJiaocaiByResourceId(resourceId);
		list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下：1）id 冲突；2）updateTime 不更新
        createJiaocaiList(resourceId, list);
    }

    private void deleteJiaocaiByResourceId(Long resourceId) {
        jiaocaiMapper.deleteByResourceId(resourceId);
    }

}